package cn.dm.item.controller;

import cn.dm.client.RestDmImageClient;
import cn.dm.client.RestDmItemTypeClient;
import cn.dm.common.Dto;
import cn.dm.common.DtoUtil;
import cn.dm.common.EmptyUtils;
import cn.dm.pojo.DmImage;
import cn.dm.pojo.DmItemType;
import cn.dm.vo.DmItemTypeVo;
import cn.dm.vo.HotItemVo;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class ItemTypeController {
    @Resource
    private RestDmItemTypeClient restDmItemTypeClient;
    @Resource
    private RestDmImageClient restDmImageClient;


    @PostMapping("/api/p/index/queryAllType")
    public Dto queryAllType() throws Exception {
        //先查询level=1的一级分类，然后遍历每个一级分类：查询其下一级分类，并且查询其对应的热门商品
        List<DmItemTypeVo> list = restDmItemTypeClient.selectTestChildren();
        //给每个热门商品，设置图片的url
        if(EmptyUtils.isNotEmpty(list)){
            for (DmItemTypeVo vo: list) {
                if(EmptyUtils.isNotEmpty(vo.getHotItems())){
                    for(HotItemVo hi : vo.getHotItems()){
                        List<DmImage> images = restDmImageClient.queryDmImageList(hi.getId(),2,1);
                        if(EmptyUtils.isNotEmpty(images)) {
                            hi.setImgUrl(images.get(0).getImgUrl());
                        }
                    }
                }
            }
        }
        return DtoUtil.returnDataSuccess(list);
    }


    @PostMapping("/api/p/index/queryTransverse")
    public Dto queryTransverse() throws Exception {
        Map param = new HashMap();
        param.put("level",1);
        List<DmItemType> list = restDmItemTypeClient.getDmItemTypeListByMap(param);
        return DtoUtil.returnDataSuccess(list);
    }


}
